<?php
error_reporting(E_ALL);
require_once('config_fj.php');
require_once('personalInfo.php');
require_once('newSearch.php');
$link=LinkMysql();


function hasAuditPermission()//检验是否有审核权利
{
	global $browser_info;
	if($browser_info->permission=='teacher' or $browser_info->permission=='administrator')
		return true;
	else
		return false;
	
}
//游客或者学生
function showAuditStation($title,$author,$state)
{
	//name格式:state###1###title###author###oldstate
	if( hasAuditPermission()){
	?>
	<select name="<?php echo "state###".$title.'###'.$author.'###'.$state;?>" style="width:100px">
	<option value="Y" <?php echo $state=='Y'?"selected='true'":"";?>>通过</option>
	<option value="N" <?php echo $state=='N'?"selected='true'":"";?>>未处理</option>
	<option value="D" >删除</option>
	</select>
	<?php
	}else{
		echo $state=='Y'?"通过":"未处理";
	}
}
function updateUnpubPaperInfo($title,$author,$state)
{
	global $link;
	$author=sqlFilter($author,true);
	$title=sqlFilter($title,true);
	if($state!='D'){
		$sql="UPDATE ".TBIDEN." SET 导师审核通过='$state' WHERE 第一作者 LIKE '%$author%' AND 标题 LIKE '%$title%' LIMIT 1";
	}else{
		$sql="DELETE FROM ".TBIDEN."  WHERE 第一作者 LIKE '%$author%' AND 标题 LIKE '%$title%' LIMIT 1";
	}
	//echo $sql.EOL;
	$ret=MysqlQuery($link,DBLABA,$sql);
	if(!$ret){
		//for debug
		echo "Sql:".$sql.EOL;
		echo "Query failed at line:".__LINE__.$link->error.EOL;
		return 0;
	}else{
		return $link->affected_rows;
	}
}

function dealWithSubmit()
{
	$change_count=0;
	foreach($_POST as $name =>$selected_state)
	{
		
		if(strstr($name,"state")){//防止处理其他变量
			$update_info=explode("###",$name);
			$title=$update_info[1];
			$author=$update_info[2];
			$old_state=$update_info[3];
			if($old_state != $selected_state){//检测是否需要更新
				$change_count+=updateUnpubPaperInfo($title,$author,$selected_state);
			}

		}
		
	}
	//for debug
	echo "Updated total record(s):".$change_count.EOL;
}

//论文审核里，作者列的作者是通过下划线连接的
function isOneofTheAuthor($name,$row)
{
	$number=array('一','二','三','四','五');
	foreach($number as $al_nu)
	{
		$index="第".$al_nu."作者";
		if(stristr($row[$index],$name)){
			return true;
		}
	}
	
	if(stristr($row['通信作者'],$name) || stristr($row['其他作者'],$name) ){
		return true;
	}
	
	return false;
	
}

function simpleUnpubDis($row,$type)
{
	if($type=='J')
		$paper_dis_link="Ach_journal.html?title=".$row['标题']."&author=".$row['第一作者']."&type=1";
	else
		$paper_dis_link="Ach_conference.html?title=".$row['标题']."&author=".$row['第一作者']."&type=1";
	$cont="<a href='$paper_dis_link' target='_blank'>".$row['标题']."</a><br/>";
	$order=array('一','二','三','四','五','六','七','八','九','十');
	$authors="";
	for($i=0;$i<count($order);++$i)
	{
		$index="第".$order[$i]."作者";
		if(!empty($row[$index])){
			$authors .= empty($authors)?"":",";
			$authors .= $row[$index];
		}
		else{
			break;
		}
	}
	$cont .= "$authors<br/>";
	$cont .= $row['期刊会议名']."</br>";
	return $cont;
}

function disRecords($ret)
{

	global $browser_info;
	$browser_name = $browser_info->name;
	$row_index=1;
?>
	<ol>
<?php
		while($row=$ret->fetch_assoc())
		{
			formatPublishedPaper($row);//格式化未发表论文已发表论文共有的部分
			$type=strstr($row['论文类型'],'期刊')?"J":"C";
			$paper_id=hash('adler32',$row['标题'].$row['第一作者']);
			$cli_op="hdShowAbs('$paper_id')";
?>
			<li>
			<div><?php echo simpleUnpubDis($row,$type); $row_index++;?><a  href="javascript:void(0);" onclick="<?php echo $cli_op;?>" >摘要</a>
			<?php 
			if(isOneofTheAuthor($browser_name,$row) || $row['申请人']==$browser_name){//如果当前浏览者是论文作者之一或者是申请人
				$edit_info="author=".$row['第一作者']."&title=".$row['标题'];?>
			<a href="unpubSubmit.php?<?php echo $edit_info;?>" target="_blank"> | 编辑</a>
			<?php
				}
			?>
			</div>
			<div id="<?php echo $paper_id;?>" style="display:none">
			<?php echo "　　".nl2br($row['摘要']);?>
			</div>
			<p>Submitted By: <?php echo $row['申请人']."　At: ".$row['申请日期'];?>　　导师审核意见：<?php showAuditStation( $row['标题'],$row['第一作者'],$row['导师审核通过']);?></p>
			
			
			</li>
			<?php
		}
			?>
	</ol>
<?php

}
//录用待发表、大修、小修、被拒、已提交、待审核
function getStates($state)
{
	global $link;
	$sql="SELECT * FROM ".TBIDEN." WHERE 当前状态 LIKE '%$state%' ORDER BY 申请日期 ASC";
	$ret=MysqlQuery($link,DBLABA,$sql);
	if(!$ret){
		//for debug
		echo "Sql:".$sql.EOL;
		echo "Query failed at line:".__LINE__.".".$link->error.EOL;
	}
	else if($ret->num_rows){
		echo "<b>$state</b>:<br/>";
		disRecords($ret);
	}
}


//显示论文
function disAll()
{
	$states=array('录用待发表','大修','小修','被拒','已提交','待审核','已发表');
	if(hasAuditPermission()){
		?>
		<form action="" method="post">
		<input type="submit" value="提交"><br/><br/>
		<?php
	}
	foreach($states as $val)
	{
		getStates($val);
	}
	if(hasAuditPermission()){
		?>
		</form>
		<?php 
	}
		
}
	
?>